// src/api/permissionAPI.js
import http from '@/utils/http'

/**
 * 添加系统权限
 * @param {Object} params - 权限信息对象
 */
export const addPermissionAPI = (params) => {
  return http.post('/sys-client/cms/sysPermission/add', params)
}

/**
 * 修改系统权限资料
 * @param {Object} params - 包含 id 和权限信息的对象
 */
export const updatePermissionAPI = (params) => {
  return http.post(`/sys-client/cms/sysPermission/update?id=${params.id}`, params.saveBO)
}

/**
 * 切换权限状态（0->1, 1->0）
 * @param {number} id - 权限ID
 */
export const togglePermissionStatusAPI = (id) => {
  return http.post(`/sys-client/cms/sysPermission/toggleStatus?id=${id}`)
}

/**
 * 删除单个系统权限
 * @param {number} id - 权限ID
 */
export const deletePermissionAPI = (id) => {
  return http.post(`/sys-client/cms/sysPermission/delete?id=${id}`)
}

/**
 * 批量删除系统权限
 * @param {string} ids - 以逗号分隔的ID字符串
 */
export const deleteBatchPermissionAPI = (ids) => {
  return http.post(`/sys-client/cms/sysPermission/deleteBatch?ids=${ids}`)
}

/**
 * 根据ID获取权限详情
 * @param {number} id - 权限ID
 */
export const getPermissionByIdAPI = (id) => {
  return http.post(`/sys-client/cms/sysPermission/get?id=${id}`)
}

/**
 * 获取所有系统权限列表（带分页）
 * @param {Object} params - 查询参数
 */
export const listAllPermissionsAPI = (params) => {
  console.log('🚀 发送请求:', {
    url: '/sys-client/cms/sysPermission/listAll',
    method: 'post',
    data: params
  })
  return http.post('/sys-client/cms/sysPermission/listAll', params).then(res => {
    console.log('✅ 接收到响应:', res)
    return res
  }).catch(err => {
    console.error('❌ 请求失败:', err)
    throw err
  })
}

/**
 * 根据权限名模糊查询权限列表
 * @param {Object} params - 查询参数对象
 */
export const searchByPermissionNameAPI = (params) => {
  return http.post('/sys-client/cms/sysPermission/searchByPermissionName', params)
}
